package com.easyjava.builder;

import com.easyjava.bean.Constants;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author: 小闹心
 * @Description: 生成工具的基类
 * @DateTime: 2024/11/17 10:28
 **/
public class BuildBase {
    private static final Logger logger = LoggerFactory.getLogger(BuildBase.class);
        public static void execute(){
        // 生成日期枚举
        build("DateTimePatternEnum",Constants.PATH_ENUMS,Constants.PACKAGE_ENUMS);
        // 生成日期工具类
        build("DateUtils", Constants.PATH_UTILS,Constants.PACKAGE_UTILS);
        // 生成mapper基础类
        build("BaseMapper", Constants.PATH_MAPPER,Constants.PACKAGE_MAPPER);
        // 生成simplePage
        build("SimplePage",Constants.PATH_QUERY,Constants.PACKAGE_QUERY);
        // 生成BaseParam类
        build("BaseParam",Constants.PATH_QUERY,Constants.PACKAGE_QUERY);
        // 生成PaginationResultVO
        build("PaginationResultVO",Constants.PATH_VO,Constants.PACKAGE_VO);
        // 生成ResultCodeEnum
        build("ResultCodeEnum",Constants.PATH_ENUMS,Constants.PACKAGE_ENUMS);
        // 生成BaseEnum
        build("BaseEnum",Constants.PATH_ENUMS,Constants.PACKAGE_ENUMS);
        // 生成WebMvcConfiguration
        build("WebMvcConfiguration",Constants.PATH_CONFIG,Constants.PACKAGE_CONFIG);
        // 生成MyBatisConfig
        build("MyBatisConfig",Constants.PATH_CONFIG,Constants.PACKAGE_CONFIG);
        // 生成Knife4jConfig
        if(Constants.KNIFE4J_ENABLE){
            build("Knife4jConfiguration", Constants.PATH_CONFIG, Constants.PACKAGE_CONFIG);
        }
        // 生成Result ResultCodeEnum 包
        List<String> imports = new ArrayList<>();
        imports.add("import " + Constants.PACKAGE_ENUMS + "." + "ResultCodeEnum;");
        build("Result",Constants.PATH_COMMON,Constants.PACKAGE_COMMON,imports);
        // 生成自定义异常类 ResultCodeEnum 包
        build("GlobalException",Constants.PATH_EXCEPTION,Constants.PACKAGER_EXCEPTION,imports);
        // 生成全局异常类捕获 Result、GlobalException 包
        imports.clear();
        imports.add("import " + Constants.PACKAGE_COMMON + "." + "Result;");
        imports.add("import " + Constants.PACKAGER_EXCEPTION + "." + "GlobalException;");
        build("GlobalExceptionHandler",Constants.PATH_CONFIG,Constants.PACKAGE_CONFIG,imports);
        imports.clear();
        imports.add("import " + Constants.PACKAGE_ENUMS + ".BaseEnum;");
        build("StringToBaseEnumConverterFactory",Constants.PATH_CONFIG,Constants.PACKAGE_CONFIG,imports);
        build("CodeEnumUtil",Constants.PATH_UTILS,Constants.PACKAGE_UTILS,imports);
        // 生成CodeEnumTypeHandler
        imports.add("import " + Constants.PACKAGE_UTILS + ".CodeEnumUtil;");
        build("CodeEnumTypeHandler",Constants.PATH_CONFIG,Constants.PACKAGE_CONFIG,imports);

    }
    private static void build(String fileName,String outPutPath,String packageName){
        File folder = new File(outPutPath);
        if(!folder.exists()){
            folder.mkdirs();
        }
        File javaFile = new File(folder,fileName + ".java");

        OutputStream out = null;
        OutputStreamWriter outWriter = null;
        BufferedWriter bw = null;

        InputStream in = null;
        InputStreamReader inReader = null;
        BufferedReader br = null;

        try{
            out = new FileOutputStream(javaFile);
            outWriter = new OutputStreamWriter(out, StandardCharsets.UTF_8);
            bw = new BufferedWriter(outWriter);

            String templatePath = BuildBase.class.getClassLoader().getResource("template/" + fileName + ".txt").getPath();
            in = new FileInputStream(templatePath);
            inReader = new InputStreamReader(in, StandardCharsets.UTF_8);
            br = new BufferedReader(inReader);
            bw.write("package " + packageName + ";");
            bw.newLine();
            bw.newLine();
            String lineInfo = null;
            while((lineInfo = br.readLine()) != null){
                bw.write(lineInfo);
                bw.newLine();
            }
            bw.flush();
        }catch (Exception e){
            logger.error("生成基础类: {} 失败" ,fileName);
        }finally {
            if(br != null){
                try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(inReader != null){
                try {
                    inReader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(in != null){
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(bw != null){
                try {
                    bw.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(outWriter != null){
                try {
                    outWriter.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(out != null){
                try {
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    private static void build(String fileName, String outPutPath, String packageName, List<String> imports){
        File folder = new File(outPutPath);
        if(!folder.exists()){
            folder.mkdirs();
        }
        File javaFile = new File(folder,fileName + ".java");

        OutputStream out = null;
        OutputStreamWriter outWriter = null;
        BufferedWriter bw = null;

        InputStream in = null;
        InputStreamReader inReader = null;
        BufferedReader br = null;

        try{
            out = new FileOutputStream(javaFile);
            outWriter = new OutputStreamWriter(out, StandardCharsets.UTF_8);
            bw = new BufferedWriter(outWriter);

            String templatePath = BuildBase.class.getClassLoader().getResource("template/" + fileName + ".txt").getPath();
            in = new FileInputStream(templatePath);
            inReader = new InputStreamReader(in, StandardCharsets.UTF_8);
            br = new BufferedReader(inReader);
            bw.write("package " + packageName + ";");
            bw.newLine();
            bw.newLine();
            for(String importStr : imports){
                bw.write(importStr);
                bw.newLine();
            }
            bw.newLine();
            String lineInfo = null;
            while((lineInfo = br.readLine()) != null){
                bw.write(lineInfo);
                bw.newLine();
            }
            bw.flush();
        }catch (Exception e){
            logger.error("生成基础类: {} 失败" ,fileName);
        }finally {
            if(br != null){
                try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(inReader != null){
                try {
                    inReader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(in != null){
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(bw != null){
                try {
                    bw.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(outWriter != null){
                try {
                    outWriter.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(out != null){
                try {
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

}
